package a66weding.com.jiehuntong.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.base.BaseActivity;
import a66weding.com.jiehuntong.pager.BasePager;
import a66weding.com.jiehuntong.pager.ToolsPager_Gift_Home;

/**
 * Created by Administrator on 2016/6/19.
 */
public class ToolsGiftsActivity extends BaseActivity {
    private static HashMap<String,BasePager> mGiftPages;
    private HomeBaseFragment mFragment;
    private boolean isEditGift;
    @Override
    protected void onPreInitViewExecute() {
        super.onPreInitViewExecute();
            mGiftPages = new HashMap<String,BasePager>();
        mGiftPages.put("礼金记账本主页",new ToolsPager_Gift_Home(this));
    }
    @Override
    protected View getContentView() {
        return null;
}
    @Override
    protected void onPostCreateExecute() {
        initData();
    }
    private void    initData() {
        setFragment("礼金记账本主页");
    }
    /**
     * 设置 Fragment 到布局中
     */
    public  void setFragment(String pagerName) {
        //管理
        FragmentManager fragmentManager = getSupportFragmentManager();
        //事物
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //Fragment
        mFragment=new HomeBaseFragment();
        Bundle mBundle= new Bundle();
        mBundle.putString("pagerName",pagerName);
        mFragment.setArguments(mBundle);
        //添加到回退栈中
        transaction.addToBackStack(pagerName);
        //把pager的视图设给BaseActivity的FrameLayout
        transaction.replace(R.id.flay_baseactivity_content, mFragment);
        transaction.commit();
    }
    public static  class HomeBaseFragment extends Fragment {
        public  HomeBaseFragment(){}
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            String pagerName = (String) getArguments().get("pagerName");
            BasePager pager = getBasePager(pagerName);
            if (null != pager)
             return pager.mRootView;
            return null;
        }
    }
    public static  BasePager getBasePager(String pagerName) {
        BasePager pager =  mGiftPages.get(pagerName);
        if (null != pager && !pager.mInit) {
            pager.mInit = true;
            pager.initData();
        }
        return pager;
    }

    public boolean isEditGift() {
        return isEditGift;
    }
    public void setEditGift(boolean editGift) {
        isEditGift = editGift;
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = { 0, 0 };
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode, event);
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(getSupportFragmentManager().getBackStackEntryCount()==1){
                finish();
                return true;
            }
        }
        return super.onKeyUp(keyCode,event);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
